﻿// <copyright file="Restriction.cs" company="Ucaya">
// </copyright>
// <author>Ucaya</author>

namespace MediaRss
{
    #region

    using System.Text;
    using System.Xml;
    using System.Xml.Serialization;

    #endregion

    [XmlRoot(ElementName = "restriction", Namespace = "http://search.yahoo.com/mrss/")]
    public class Restriction : MediaRssBase, IXmlSerializable
    {
        #region Constants and Fields

        internal const string ELEMENT_NAME = "restriction";

        #endregion

        #region Public Properties

        public string Relationship { get; set; }

        public string Type { get; set; }

        #endregion

        #region Public Methods

        public void ReadXml(XmlReader reader)
        {
            bool isEmptyElement = reader.IsEmptyElement;
            if (reader.HasAttributes)
            {
                for (int i = 0; i < reader.AttributeCount; i++)
                {
                    reader.MoveToNextAttribute();
                    if (reader.NamespaceURI == "")
                    {
                        if (reader.LocalName == "type")
                        {
                            this.Type = reader.Value;
                        }
                        else if (reader.LocalName == "relationship")
                        {
                            this.Relationship = reader.Value;
                        }
                        else
                        {
                            base.AttributeExtensions.Add(new XmlQualifiedName(reader.LocalName, reader.NamespaceURI), reader.Value);
                        }
                    }
                }
            }
            reader.ReadStartElement();
            base.AddElementExtensions(reader, isEmptyElement);
        }

        public override string ToString()
        {
            var builder = new StringBuilder();
            builder.AppendFormat("Type: {0}\n", this.Type);
            builder.AppendFormat("Relationship: {0}\n", this.Relationship);
            if (base.ElementExtensions.Count > 0)
            {
                foreach (var element in base.ElementExtensions)
                {
                    builder.AppendLine(element.ToString());
                }
            }
            return builder.ToString();
        }

        public void WriteXml(XmlWriter writer)
        {
            if (this.Type != null)
            {
                writer.WriteAttributeString("type", null, this.Type);
            }
            if (this.Relationship != null)
            {
                writer.WriteAttributeString("relationship", null, this.Relationship);
            }
            foreach (var pair in base.AttributeExtensions)
            {
                writer.WriteAttributeString(pair.Key.Name, pair.Key.Namespace, pair.Value);
            }
            foreach (var element in base.ElementExtensions)
            {
                element.WriteTo(writer);
            }
        }

        #endregion
    }
}